//
//  CityController.m
//  BrushFace
//
//  Created by 吕海瑞 on 15/12/29.
//  Copyright © 2015年 Zzz. All rights reserved.
//
#import "MeRequest.h"
#import "CityController.h"
#import "CitySubVC.h"
@interface CityController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray *subCitys;

@end

@implementation CityController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (_VCType == cityHome ||_VCType == cityLive)
    {
        [self.navBar setTitle:@"城市"];
//        NSString *path = [[NSBundle mainBundle]pathForResource:@"City" ofType:@"plist"];
//        _subCitys = [[NSArray alloc]initWithContentsOfFile:path];
       // _dataDic = [[NSDictionary alloc]initWithContentsOfFile:path];
        
       // _allKeys = [[_dataDic allKeys] sortedArrayUsingSelector:
                  //  @selector(compare:)];
        NSString *path = [[NSBundle mainBundle]pathForResource:@"address" ofType:@"json"];
        NSData *data = [[NSData alloc]initWithContentsOfFile:path];;
        id jsondata = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        _subCitys = (NSArray *)jsondata;
        
    }
    else
    {
        [self.navBar setTitle:@"行业"];
     NSString *path = [[NSBundle mainBundle]pathForResource:@"calling" ofType:@"plist"];
        _subCitys = [[NSArray alloc]initWithContentsOfFile:path];
    }
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
       _table.delegate = self;
    _table.dataSource = self;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//    if (_VCType == city)
//    {
//    return _allKeys.count;
//    }
//    else
//    {
//       return  1;
//    }
    return  1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    if (_VCType == city)
//    {
//    _subCitys = [_dataDic objectForKey:[_allKeys objectAtIndex:section]];
//    return _subCitys.count ;
//    }
//    else
//    {
//        return _subCitys.count;
//    }
   return _subCitys.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (_VCType == cityHome ||_VCType == cityLive)
    {
       return @"全部";
 // return [_allKeys objectAtIndex:section];
    }
    else
    {
        return nil;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellName = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    if (_VCType == cityHome ||_VCType == cityLive)
    {

    //_subCitys = [_dataDic objectForKey:[_allKeys objectAtIndex:indexPath.section]];
        NSDictionary *dic = [_subCitys objectAtIndex:indexPath.row];
    cell.textLabel.text = dic[@"name"];
    }
    else
    {
     cell.textLabel.text = [_subCitys objectAtIndex:indexPath.row];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_VCType == cityHome ||_VCType == cityLive)
    {
        //地址  省市
        CitySubVC *citySubVC = [[CitySubVC alloc]init];
        NSDictionary *dic = [_subCitys objectAtIndex:indexPath.row];
        if (_VCType == cityHome)
        {
             citySubVC.VCType = cityHome1;
        }
        else if (_VCType == cityLive1)
        {
         citySubVC.VCType = cityLive1;
        }
       
        citySubVC.subCitys = dic[@"items"];
        citySubVC.proCode = dic[@"value"];
        citySubVC.proName = dic[@"name"];
        [self.navigationController pushViewController:citySubVC animated:YES];

    }
    else if (_VCType ==calling)
    {
        //上传职业  行业
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        [params setObject:@(indexPath.row+1) forKey:@"industry"];
        [MeRequest sendUserDataWithType:industry AndUserData:params success:^(NSDictionary *dic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSString *string = [NSString stringWithFormat:@"%@",dic[@"result"]];
                if ([string intValue]==1)
                {
                    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                    _calling(cell.textLabel.text);
                    [self.navigationController popViewControllerAnimated:YES];
                    
                }});
        } error:^(NSError *error) {
            
        }];
    
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
